﻿using Abp.Application.Services.Dto;
using Abp.Authorization.Users;
using Abp.Domain.Repositories;
using Abp.Domain.Uow;
using AbpProject.Authorization.Users;
using AbpProject.Menus.Dto;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AbpProject.InitializationTenants
{
    public class InitializationTenantAppService : AbpProjectAppServiceBase, IInitializationTenantAppService
    {
        private readonly IUnitOfWorkManager _unitOfWorkManager;
        private readonly IRepository<AbpMenu, long> _menuRepository;
        private readonly IRepository<AbpRoleMenu, long> _roleMenuRepository;
        private readonly IRepository<UserRole, long> _userRoleRepository;
        public InitializationTenantAppService(IUnitOfWorkManager unitOfWorkManager, IRepository<UserRole, long> userRoleRepository,
            IRepository<AbpMenu, long> menuRepository,
             IRepository<AbpRoleMenu, long> roleMenuRepository)
        {
            _unitOfWorkManager = unitOfWorkManager;
            _userRoleRepository = userRoleRepository;
            _menuRepository = menuRepository;
            _roleMenuRepository = roleMenuRepository;
        }
        /// <summary>
        /// 初始化菜单
        /// </summary>
        /// <returns></returns>
        public async Task InitializationMenu()
        {

            var tenantId = AbpSession.TenantId;
            var roleId = _userRoleRepository.FirstOrDefault(r => r.TenantId == tenantId);
            #region 生成菜单
            var menu = await GetMenu();

            var parentMenu = menu.Where(r => r.ParentId == 0 || !r.ParentId.HasValue);
            foreach (var item in parentMenu)
            {
                var oldId = item.Id;
                await InsertMenu(item, menu, oldId, roleId.RoleId, tenantId, 0);
                #endregion
            }

        }

        private async Task InsertMenu(AbpMenu input, List<AbpMenu> list, long oldId, long roleId, int? tenantId, long? parentId)
        {
            input.Id = 0;
            input.TenantId = tenantId;
            input.ParentId = parentId;
            var newId = _menuRepository.InsertAndGetId(input);
            InsertRoleMenu(newId, roleId, tenantId);
            var childrenMenu = list.Where(r => r.ParentId == oldId).ToList();
            foreach (var itemchildren in childrenMenu)
            {
                await InsertMenu(itemchildren, list, itemchildren.Id, roleId, tenantId, newId);
            }

        }

        private async Task<List<AbpMenu>> GetMenu()
        {
            List<string> list = new List<string>();
            list.Add(AbpDataFilters.MayHaveTenant);
            list.Add(AbpDataFilters.MustHaveTenant);
            var res = new List<AbpMenu>();
            using (_unitOfWorkManager.Current.DisableFilter(list.ToArray()))//禁用MayHaveTenant  MustHaveTenant过滤
            {
                res = _menuRepository.GetAll().AsNoTracking().Where(r => r.TenantId == null).ToList();
            }
            return res;
        }

        private void InsertRoleMenu(long MenuId, long RoleId, int? TenantId)
        {
            var rolemenu = new AbpRoleMenu
            {
                MenuId = MenuId,
                RoleId = RoleId,
                TenantId = TenantId
            };
            _roleMenuRepository.Insert(rolemenu);
        }


    }
}
